import React from 'react';
import axios from 'axios';
import Handline from './headline';
const newUrl =
  "http://newsapi.org/v2/top-headlines?country=us&apiKey=209cd02e74314a32a4e5f1d5b9cbdff1";

interface Props {
  dataList?: any[];
  newUrl?: string;
  status?: any;
};

interface State {
  dataList: any[];
  status: any;
  test:any
};
export default class NewList extends React.Component<Props, State> {
  mounted: boolean;
  time: any;
  static val:any = 1234;
  // val
  constructor(props){
    super(props)
    this.state = {
      dataList:this.props.dataList || [],
      status:this.props.status || false,
      test:null
    }
  }

  static async getInitData(){
    // let res:any = this.val;
   return await axios.get(newUrl)
    .then((response) => {
      // console.log("ctx========",response.data.articles)
      if (response.status === 200) {
           response;
           console.log("response",response)
      }
     
      return  response;
    });
  }
   componentDidMount(){
    if(!this.props.dataList){
      axios.get(newUrl)
      .then((response) => {
        // console.log("ctx========",response.data.articles)
        if (response.status === 200) {
          this.setState({
            dataList: response.data.articles,
            status: true
          });
        }
  
        return  response;
      });
    }
   
  }
  getdatas(){
    import('./test.js').then((data)=>{
      return data.default;
    })
  }
  render() {
    let { dataList, status } =  this.state;
    return (
      <div>
        <h1> 新闻列表 {this.getdatas()} </h1>
        <div>
          {status ? dataList.map((item, key) => {
            return <Handline key={key} {...item} />
          }) : <div>loading...</div>}

        </div>

      </div>
    );
  }
}